package com.web.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.dao.FeedbackMapper;
import com.web.dao.MemberMapper;
import com.web.dao.MessageMapper;
import com.web.domain.WebResponse;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.domain.dataTable.BootstrapTableResponse;
import com.web.dto.FeedbackDto;
import com.web.entity.Feedback;
import com.web.entity.Member;
import com.web.entity.Message;
import com.web.manager.jpush.SendJPushMessage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author FDjavaone
 * @date 2017/9/30
 */
@Controller
public class WebFeedBackController {
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MessageMapper messageMapper;


    @RequestMapping(value = "sysm/feedback",
            method = RequestMethod.GET)
    public String syso(Model model, HttpServletRequest request) {
        return "feedback";
    }


    @Data
    public static class FeedbackColumns extends BootstrapTableColumns {


    }


    @RequestMapping("/sysm/getFeedback")
    @ResponseBody
    public BootstrapTableResponse getFeedback(FeedbackColumns feedbackColumns) {
        PageHelper.startPage(feedbackColumns.getPageNumber(), feedbackColumns.getPageSize());
        Page<FeedbackDto> page = feedbackMapper.selectAllFeedbacks(feedbackColumns);
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sysm/feedback/return")
    @ResponseBody
    public WebResponse getDetail(Feedback feedback) {
        Feedback old = feedbackMapper.selectByPrimaryKey(feedback.getId());
        Date date = new Date();
        Member member = memberMapper.selectByPrimaryKey(old.getmId());
        if (member != null) {
            Message message = new Message();
            message.setTargetName(member.getName());
            message.setTarget(member.getId());
            message.setFromName("系统");
            message.setFrom(0L);
            message.setTitle("反馈回复");
            message.setContent(feedback.getContent());
            message.setCreateTime(date);
            message.setModifyTime(date);
            messageMapper.insertSelective(message);
            SendJPushMessage.jPushByMessage(message);
            return WebResponse.ok("操作成功");
        } else {
            return WebResponse.ok("反馈回复接收人有误！");
        }

    }

}
